package com.jiangyg.mall.ware.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.ware.dto.PurchaseDetailCombineDTO;
import com.jiangyg.mall.ware.dto.PurchaseDetailDTO;
import com.jiangyg.mall.ware.service.PurchaseDetailService;
import com.jiangyg.mall.ware.vo.PurchaseDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：采购详细需求控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-30 07:41:17
 */
@RestController
@RequestMapping("/ware/purchase-detail")
public class PurchaseDetailController {

    /**
     * 接口
     */
    private final PurchaseDetailService purchaseDetailService;

    @Autowired
    public PurchaseDetailController(PurchaseDetailService purchaseDetailService) {
        this.purchaseDetailService = purchaseDetailService;
    }

    /**
     * 功能描述：分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<PurchaseDetailVO>> select(HttpServletRequest request, @RequestBody Pagination<PurchaseDetailVO> pagination) throws Exception {
        return Result.ok(purchaseDetailService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改
     *
     * @param request 请求
     * @param dto 参数
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated PurchaseDetailDTO dto) throws Exception {
        return Result.saveOrUpdate(purchaseDetailService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取
     *
     * @param request          请求
     * @param purchaseDetailId ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<PurchaseDetailVO> info(HttpServletRequest request, @RequestParam(name = "purchaseDetailId") Long purchaseDetailId) throws Exception {
        return Result.ok(purchaseDetailService.selectById(purchaseDetailId));
    }

    /**
     * 功能描述：删除
     *
     * @param request           请求
     * @param purchaseDetailIds 批量删除的ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> purchaseDetailIds) {
        return Result.delete(purchaseDetailService.delete(purchaseDetailIds));
    }

    /**
     * 功能描述：合并采购单
     *
     * @param request 请求
     * @param dto 参数
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/combine")
    public Result<Object> combine(HttpServletRequest request, @RequestBody @Validated PurchaseDetailCombineDTO dto) {
        return Result.saveOrUpdate(purchaseDetailService.combine(dto));
    }

}
